package cn.iocoder.yudao.module.shop.controller.app.orderfeedback;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import cn.iocoder.yudao.module.pay.controller.admin.wallet.vo.recharge.PayWalletRechargePageReqVO;
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletDO;
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletRechargeDO;
import cn.iocoder.yudao.module.pay.service.wallet.PayWalletRechargeService;
import cn.iocoder.yudao.module.pay.service.wallet.PayWalletService;
import cn.iocoder.yudao.module.shop.controller.admin.giftcard.vo.ShopGiftCardRespVO;
import cn.iocoder.yudao.module.shop.controller.admin.member.vo.MemberRechargeRespVO;
import cn.iocoder.yudao.module.shop.controller.admin.orderfeedback.vo.ShopOrderFeedbackUpdateReqVO;
import cn.iocoder.yudao.module.shop.convert.giftcard.ShopGiftCardConvert;
import cn.iocoder.yudao.module.shop.convert.giftitem.ShopGiftItemConvert;
import cn.iocoder.yudao.module.shop.dal.dataobject.giftcard.ShopGiftCardDO;
import cn.iocoder.yudao.module.shop.dal.dataobject.giftitem.ShopGiftItemDO;
import cn.iocoder.yudao.module.shop.dal.dataobject.orderfeedback.ShopOrderFeedbackDO;
import cn.iocoder.yudao.module.shop.service.masterstatistics.ShopMasterStatisticsService;
import cn.iocoder.yudao.module.shop.service.orderfeedback.ShopOrderFeedbackService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 App - 评价反馈")
@RestController
@RequestMapping("/shop/orderfeedback")
@RequiredArgsConstructor
@Validated
@Slf4j
public class AppOrderFeedbackController {

    @Resource
    private ShopOrderFeedbackService shopOrderFeedbackService;

    @Resource
    private ShopMasterStatisticsService shopMasterStatisticsService;

    @Resource
    private PayWalletRechargeService payWalletRechargeService;

    @Resource
    private PayWalletService payWalletService;

    @Resource
    private MemberUserApi memberUserApi;



    @GetMapping("/orderid")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
    @Operation(summary = "获取反馈订单")
    public CommonResult<ShopOrderFeedbackDO> getByOrderId(@RequestParam("id") Long id){
        ShopOrderFeedbackDO orderFeedbackDO = shopOrderFeedbackService.getByOrderId(id);
        return success(orderFeedbackDO);
    }

    @PutMapping("/confirm")
    @Operation(summary = "确认顾客订单反馈")
    @Transactional(rollbackFor = Exception.class)
    public CommonResult<Boolean> confirmOrderFeedback(@Valid @RequestBody ShopOrderFeedbackUpdateReqVO updateReqVO) {
        ShopOrderFeedbackDO shopOrderFeedbackDO = shopOrderFeedbackService.confirm(updateReqVO);
        shopMasterStatisticsService.recordFeedback(shopOrderFeedbackDO);
        return success(true);
    }

    @PostMapping("/recharge-page")
    @Operation(summary = "充值记录页")
    public CommonResult<PageResult<MemberRechargeRespVO>> page(@Valid @RequestBody PayWalletRechargePageReqVO pageReqVO){
        MemberUserRespDTO member = memberUserApi.getUser(pageReqVO.getMemberId());
        if(member != null){
            pageReqVO.setTenantId(member.getTenantId());
        }

        PageResult<PayWalletRechargeDO> pageResult = payWalletRechargeService.getOrderPage(pageReqVO);

        List<Long> walletIds = CollectionUtils.convertList(pageResult.getList(), PayWalletRechargeDO::getWalletId);
        List<PayWalletDO> walltList = payWalletService.getWallets(walletIds);

        List<Long> userIds = CollectionUtils.convertList(walltList, PayWalletDO::getUserId);
        List<MemberUserRespDTO> memberList = memberUserApi.getUserList(userIds);

        Map<Long, MemberUserRespDTO> memberMap =  CollectionUtils.convertMap(memberList, MemberUserRespDTO::getId);

        Map<Long, MemberUserRespDTO> walletMemberMap = new HashMap<>();
        for(int i = 0; i<walltList.size(); i++){
            PayWalletDO walletDO = walltList.get(i);
            if(!walletMemberMap.containsKey(walletDO.getId())){
                walletMemberMap.put(walletDO.getId(), memberMap.get(walletDO.getUserId()));
            }
        }

        List<MemberRechargeRespVO> list = new ArrayList<>();

        for(int i = 0; i<pageResult.getList().size(); i++){
            PayWalletRechargeDO recharge = pageResult.getList().get(i);
            MemberRechargeRespVO respVO = new MemberRechargeRespVO();
            respVO.setRecharge(recharge);
            respVO.setMember(walletMemberMap.get(recharge.getWalletId()));

            list.add(respVO);
        }

        return success(new PageResult<>(list, pageResult.getTotal()));
    }
}
